package com.ktg.service.impl;

import com.ktg.ktgcommon.domain.mainData.MdItemType;
import com.ktg.ktgcommon.filter.TokenVerifyFilter;
import com.ktg.mapper.MDItemTypeMapper;
import com.ktg.service.MDItemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MDItemTypeServiceImpl implements MDItemTypeService {

    @Autowired
    private MDItemTypeMapper mdItemTypeMapper;

    @Override
    public List<MdItemType> CheckMdItemTypeAll() {
        List<MdItemType> list = mdItemTypeMapper.CheckMdItemTypeAll();
        return list;
    }

    @Override
    public List<MdItemType> checkMdItemTypeCondition(String enableFlag, String itemTypeName) {
        List<MdItemType> mdItemType = mdItemTypeMapper.checkMdItemTypeCondition(enableFlag, itemTypeName);
        return mdItemType;
    }

    @Override
    public Integer delProduct(Integer itemTypeId) {
        //根据id删除指定数据,但是如果下面有子节点不能进行删除。需要下面没有子节点才能进行删除
        //根据传递进来的id来查询是否下面有子节点
        List<MdItemType> mdItemTypeList = mdItemTypeMapper.checkChildren(itemTypeId);
        if (mdItemTypeList.isEmpty()) {
            Integer integer = mdItemTypeMapper.delProduct(itemTypeId);
            return integer;
        } else {
            return 0;
        }
    }

    @Override
    public MdItemType checkProductById(Integer itemTypeId) {
        MdItemType mdItemType = mdItemTypeMapper.checkProductById(itemTypeId);
        return mdItemType;
    }

    @Override
    public MdItemType checkProductByParentId(Long parentTypeId) {
        MdItemType mdItemType = mdItemTypeMapper.checkProductByParentId(parentTypeId);
        return mdItemType;
    }

    @Override
    public Integer addProduct(MdItemType mdItemType) {
        //生成随机数
        Random random = new Random();
        // 生成一个1到100000之间的随机整数
        int min = 1;
        int max = 100000;
        int randomNumber = random.nextInt(max - min + 1) + min;
        mdItemType.setItemTypeCode(String.valueOf(randomNumber));
        //解析jwt获取user信息
        TokenVerifyFilter tokenVerifyFilter = new TokenVerifyFilter();
        //设置创建时间
        mdItemType.setCreateTime(new Date());
        //设置创建者名称名称
        mdItemType.setCreateBy(tokenVerifyFilter.getUserName().getUsername());
        //循环查询父id 如果查询到父id为0则查到了顶级节点，不需要继续查询了
        List<Long> allParentTypeIds = getAllParentTypeIds(mdItemType);
        System.out.println("============");
        System.out.println(allParentTypeIds);
        //把allParentTypeIds转化为一串字符串
        String allParentTypeIdsString = String.join(",", allParentTypeIds.stream()
                .map(Object::toString)
                .toArray(String[]::new));
        //设置值进去
        mdItemType.setAncestors(allParentTypeIdsString);
        Integer integer = mdItemTypeMapper.addProduct(mdItemType);
        return integer;
    }

    @Override
    public Integer changProduct(MdItemType mdItemType) {
        //解析jwt获取user信息
        TokenVerifyFilter tokenVerifyFilter = new TokenVerifyFilter();
        //设置修改时间
        mdItemType.setUpdateTime(new Date());
        //设置修改者名称
        mdItemType.setUpdateBy(tokenVerifyFilter.getUserName().getUsername());
        System.out.println(mdItemType);
        Integer integer = mdItemTypeMapper.changProduct(mdItemType);
        return integer;
    }


    //递归方法获取父节点
    public List<Long> getAllParentTypeIds(MdItemType mdItemType) {
        List<Long> parentList = new ArrayList<>();
        Long currentParentTypeId = mdItemType.getParentTypeId();

        while (currentParentTypeId != null && currentParentTypeId != 0) {
            MdItemType parentItem = mdItemTypeMapper.checkProductByParentId(currentParentTypeId);
            if (parentItem == null) {
                // 如果找不到父节点，则停止循环
                break;
            }
            parentList.add(parentItem.getItemTypeId());
            currentParentTypeId = parentItem.getParentTypeId();
        }
        // 添加根节点（parentTypeId 为 0 的节点）
        if (currentParentTypeId == 0) {
            parentList.add(0L); // 或者你可以添加一个表示根节点的对象或ID
        }
        // 如果需要从最远的祖先到最近的父节点排序，可以反转列表
        Collections.reverse(parentList);

        return parentList;
    }
}
